package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.MmochatActivityInfo;
import com.dragon.mmochat.model.enumType.MmochatTaskType;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.map.MmochatMapPoint;
import com.dragon.mmochat.model.role.MmochatNpc;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.task.MmochatTask;
import com.dragon.mmochat.service.MmochatMapService;
import com.dragon.mmochat.service.MmochatNpcService;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_4_4S extends SkymobiProtocolMessage {
	MmochatPlayer me = null;
	List<MmochatTask> unfinishedTaskList = new ArrayList<MmochatTask>(); // 当前任务
	List<MmochatTask> canAcceptTaskList = new ArrayList<MmochatTask>(); // 可接任务
	List<MmochatActivityInfo> dailyActivities = new ArrayList<MmochatActivityInfo>();// 日常活动
	List<MmochatActivityInfo> suggestActivities = new ArrayList<MmochatActivityInfo>();// 推荐活动

	public Mmochat_4_4S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_4_4);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		// 下发当前任务、可接任务、日常活动、推荐活动
		if (me == null) {
			return;
		}
		// 当前任务:任务ID、任务名、任务进度、交任务NPC名字、NPC所在mapId,x,y
		out.putShort((short) unfinishedTaskList.size());
		for (MmochatTask task : unfinishedTaskList) {
			// 任务ID
			out.putInt(task.getTaskId());
			// 任务名
			out.putPrefixedString(task.getTaskName(), 1);
			// 任务进度
			if (task.getTaskType() != MmochatTaskType.过渡任务
					&& task.getTaskType() != MmochatTaskType.寻宝任务
					&& task.getFinish_need_battles() == null
					&& me.canFinishTask(task.getTaskId())) {
				out.putPrefixedString("可交付", 1);
			} else {
				out.putPrefixedString("进行中", 1);
			}

			// 交任务NPC名字
			MmochatNpc npc = MmochatNpcService.getNpcById(task
					.getFinish_npcId());
			if (npc != null) {
				out.putPrefixedString(npc.getName(), 1);
			} else {
				out.putPrefixedString("-", 1);
			}

			// 任务寻路信息
			MmochatMapPoint guider = task.getGuiderPoint(me);
			if (guider != null) {
				out.putInt(guider.getMapId());
				out.putShort((short) guider.getX());
				out.putShort((short) guider.getY());
			} else {
				out.putInt(-1);
				out.putShort((short) 0);
				out.putShort((short) 0);
			}
		}

		// 可接任务:任务ID、任务名、接任务NPC、NPC所在mapId,x,y
		int max = Math.min(canAcceptTaskList.size(), 100);
		out.putShort((short) max);
		int count = 0;
		for (MmochatTask task : canAcceptTaskList) {
			if (count >= max) {
				break;
			}
			count++;
			// 任务ID
			out.putInt(task.getTaskId());
			// 任务名
			out.putPrefixedString(task.getTaskName(), 1);

			// 接任务NPC名字
			MmochatNpc npc = MmochatNpcService.getNpcById(task
					.getAccept_npcId());
			if (npc != null) {
				out.putPrefixedString(npc.getName(), 1);
			} else {
				out.putPrefixedString("-", 1);
			}

			// 任务寻路信息
			MmochatMapPoint guider = task.getGuiderPoint(me);
			if (guider != null) {
				out.putInt(guider.getMapId());
				out.putShort((short) guider.getX());
				out.putShort((short) guider.getY());
			} else {
				out.putInt(-1);
				out.putShort((short) 0);
				out.putShort((short) 0);
			}
		}

		// 日常活动:活动ID、活动名、活动时间
		out.putShort((short) dailyActivities.size());
		for (MmochatActivityInfo t : dailyActivities) {
			out.putInt(t.getId());
			out.putPrefixedString(t.getName(), 1);
			out.putPrefixedString(t.getActivityTime(), 1);
		}

		// 推荐活动：活动ID、活动名、活动奖励、活动NPC名字、NPC所在mapId,x,y
		out.putShort((short) suggestActivities.size());
		for (MmochatActivityInfo t : suggestActivities) {
			out.putInt(t.getId());
			out.putPrefixedString(t.getName(), 1);
			out.putPrefixedString(t.getActivityAward(), 1);
			MmochatNpc npc = null;
			if (t.getActivityNpcId() != null) {
				npc = MmochatNpcService.getNpcById(t.getActivityNpcId());
			}
			if (npc != null) {
				MmochatMap map = MmochatMapService.maps.get(npc.getMapId());
				if (map != null) {
					// NPC名字
					out.putPrefixedString(npc.getName(), 1);
					// NPC所在地图ID，X，Y
					out.putInt(npc.getMapId());
					out.putShort(npc.getX());
					out.putShort(npc.getY());
					continue;
				}
			}
			// NPC名字
			out.putPrefixedString(t.getActivityNpcName(), 1);
			// NPC所在地图ID，X，Y
			out.putInt(-1);
			out.putShort((short) 0);
			out.putShort((short) 0);
		}
	}

	public void setUnfinishedTaskList(List<MmochatTask> unfinishedTaskList) {
		this.unfinishedTaskList = unfinishedTaskList;
	}

	public void setCanAcceptTaskList(List<MmochatTask> canAcceptTaskList) {
		this.canAcceptTaskList = canAcceptTaskList;
	}

	public void setDailyActivities(List<MmochatActivityInfo> dailyActivities) {
		this.dailyActivities = dailyActivities;
	}

	public void setSuggestActivities(List<MmochatActivityInfo> suggestActivities) {
		this.suggestActivities = suggestActivities;
	}

	public void addUnfinishedTaskList(MmochatTask t) {
		unfinishedTaskList.add(t);
	}

	public void addCanAcceptTaskList(MmochatTask t) {
		canAcceptTaskList.add(t);
	}

	public void addDailyActivities(MmochatActivityInfo t) {
		dailyActivities.add(t);
	}

	public void addSuggestActivities(MmochatActivityInfo t) {
		suggestActivities.add(t);
	}

	public void setMe(MmochatPlayer me) {
		this.me = me;
	}

}
